package top.haichi.webpaper.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.haichi.webpaper.entity.Paper;
import top.haichi.webpaper.entity.Result;
import top.haichi.webpaper.entity.Student;
import top.haichi.webpaper.mapper.PaperMapper;
import top.haichi.webpaper.service.FileService;
import top.haichi.webpaper.utils.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.http.HttpRequest;
import java.nio.file.Paths;

@RestController
@Slf4j

//文件上传控制器
public class FileUploadController {
    @Autowired
    private FileService fileService;
    @Autowired
    private PaperMapper paperMapper;
    @Value("${upload.directory}")
    private String uploadDirectory;

    /**
     * 头像上传与修改
     *
     * @param id
     * @param image
     * @return
     * @throws IOException
     */
    @PostMapping("/file/upload/portrait")
    public Result uploadPortrait(String id, @RequestParam(value = "image") MultipartFile image) throws IOException {
        return fileService.uploadPortrait(id, image);
    }

    /**
     * 论文上传
     *
     * @param id
     * @param file
     * @param paper
     * @return
     */
    @PostMapping("/file/upload/student/paper")
    public Result uploadPaper(String id, String signup, @RequestParam(value = "file") MultipartFile file, Paper paper) {
        if (!signup.equals("已签名")) {
            return Result.error("未签名");
        }
        return fileService.uploadPaper(id, paper, file);
    }

    @PostMapping("/file/upload/teacher/paper")
    public Result uploadPaper(String status, String id, @RequestParam(required = false) MultipartFile file) {
        Paper selectedPaper = paperMapper.selectById(id);
        if (!(status.equals("审核中") || status.equals("待修改") || status.equals("已完成"))) {
            return Result.error("你干嘛");
        }

        selectedPaper.setStatus(status);
        String uuid = selectedPaper.getId();
        String fileName = file.getOriginalFilename();
        String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
        String filePath = uploadDirectory + "/files/papers/" + uuid + "." + fileType;
        selectedPaper.setType(fileType);
        selectedPaper.setSize((int) file.getSize());

        try {
            FileUtils.createFile(filePath);
            file.transferTo(Paths.get(filePath));
            log.info(id + "上传了论文在" + filePath);
            paperMapper.updateById(selectedPaper);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("论文上传失败");
        }
//        paper.setOwnerId(id);
//        if(paper.getTitle()==null||paper.getTitle().equals("")){
//            return Result.error("标题不能为空");
//        }
//        Map<String, Object> titleMap = new HashMap<>();
//        titleMap.put("title",paper.getTitle());
//        titleMap.put("owner_id",paper.getOwnerId());
//        if(!paperMapper.selectByMap(titleMap).isEmpty()){
//            return Result.error("你已经上传过同名的论文了");
//        }
////        if(paper.getCollegeId()==null||paper.getCollegeId().equals("")){
////            return Result.error("学院不能为空");
////        }
//        if(paper.getTeacherId()==null||paper.getTeacherId().equals("")){
//            return Result.error("教师不能为空");
//        }
//        if(!file.getOriginalFilename().endsWith(".pdf")){
//            return Result.error("文件格式不正确");
//        }
//
//        try {
//            paper.setOwner(studentMapper.selectById(paper.getOwnerId()).getRealName());
//            paper.setTeacher(teacherMapper.selectById(paper.getTeacherId()).getName());
//        }catch (Exception e){
//            e.printStackTrace();
//            return Result.error("非法请求");
//        }
//
//        Student student = studentMapper.selectById(id);
//        String fileName = student.getId() +"+"+ student.getName() +"+"+ paper.getTitle()+".pdf";
//
//        String filePath = uploadDirectory + "/resources/" + id + "/papers/"+fileName;
//
//        try {
//            FileUtils.createFile(filePath);
//            file.transferTo(new File(filePath));
//            paperMapper.insert(paper);
//
//        } catch (IOException e) {
//            e.printStackTrace();
        return Result.success("修改成功");

    }


}
